package com.floreantpos.ui.report;

import java.util.Date;

import com.floreantpos.main.Application;
import com.floreantpos.swing.MessageDialog;
import com.floreantpos.ui.dialog.POSMessageDialog;
import com.floreantpos.ui.util.UiUtil;
import com.floreantpos.util.POSUtil;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ReportViewer extends javax.swing.JPanel {

  private Report report;
  
  Boolean isGuestReport = false;

  public ReportViewer() {
    initComponents();
  }

  public ReportViewer(Report report) {
    initComponents();

    setReport(report);
  }
  
  public ReportViewer(Report report, Boolean enableGuestField) {
    
    isGuestReport = enableGuestField;
    
    initComponents();

    setReport(report);
  }  

  private void initComponents() {
    reportConstraintPanel = new com.floreantpos.swing.TransparentPanel();
    jLabel1 = new javax.swing.JLabel();
    cbReportType = new javax.swing.JComboBox();
    jLabel2 = new javax.swing.JLabel();
    dpStartDate = UiUtil.getCurrentMonthStart();
    dpEndDate = UiUtil.getCurrentMonthEnd();
    jLabel3 = new javax.swing.JLabel();
    btnRefresh = new javax.swing.JButton();
    jLabel4 = new javax.swing.JLabel();
    tfGuestId = new javax.swing.JTextField();
    reportPanel = new com.floreantpos.swing.TransparentPanel();

    setLayout(new java.awt.BorderLayout(5, 5));

    jLabel1.setText("Relatório" + ":");

    cbReportType.setModel(new javax.swing.DefaultComboBoxModel(new String[]{com.floreantpos.POSConstants.PREVIOUS_SALE_AFTER_DRAWER_RESET_, com.floreantpos.POSConstants.SALE_BEFORE_DRAWER_RESET}));

    jLabel2.setText(com.floreantpos.POSConstants.START_DATE + ":");

    jLabel3.setText(com.floreantpos.POSConstants.END_DATE + ":");
    
    jLabel4.setText("Cliente Núm.:");
    
    jLabel4.setEnabled(isGuestReport);
    tfGuestId.setEnabled(isGuestReport);

    btnRefresh.setText(com.floreantpos.POSConstants.REFRESH);
    btnRefresh.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        doRefreshReport(evt);
      }
    });

    org.jdesktop.layout.GroupLayout reportConstraintPanelLayout = new org.jdesktop.layout.GroupLayout(reportConstraintPanel);
    reportConstraintPanel.setLayout(reportConstraintPanelLayout);  
    
    reportConstraintPanelLayout.setHorizontalGroup(
      reportConstraintPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(reportConstraintPanelLayout.createSequentialGroup()
        .addContainerGap()
        .add(reportConstraintPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
          .add(btnRefresh, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
          .add(reportConstraintPanelLayout.createSequentialGroup()
            .add(reportConstraintPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
              .add(reportConstraintPanelLayout.createSequentialGroup()
                .add(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(dpStartDate, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
              .add(reportConstraintPanelLayout.createSequentialGroup()
                .add(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(cbReportType, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
            .add(reportConstraintPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
              .add(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
              .add(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(reportConstraintPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
              .add(dpEndDate, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
              .add(tfGuestId, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE))))
        .addContainerGap(38, Short.MAX_VALUE))
    );
    
    reportConstraintPanelLayout.setVerticalGroup(
      reportConstraintPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(reportConstraintPanelLayout.createSequentialGroup()
        .addContainerGap()
        .add(reportConstraintPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
          .add(jLabel1)
          .add(cbReportType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .add(jLabel4)
          .add(tfGuestId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
        .add(reportConstraintPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
          .add(jLabel2)
          .add(dpStartDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .add(jLabel3)
          .add(dpEndDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
        .add(btnRefresh)
        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    ); 

    reportConstraintPanelLayout.linkSize(new java.awt.Component[]{dpEndDate, dpStartDate, jLabel2, jLabel3}, org.jdesktop.layout.GroupLayout.VERTICAL);

    add(reportConstraintPanel, java.awt.BorderLayout.NORTH);

    reportPanel.setLayout(new java.awt.BorderLayout());

    add(reportPanel, java.awt.BorderLayout.CENTER);

  }

  private void doRefreshReport(java.awt.event.ActionEvent evt) {
    Date fromDate = dpStartDate.getDate();
    Date toDate = dpEndDate.getDate();
    
    Integer guestId = 0;
    if (isGuestReport && POSUtil.isNumeric(tfGuestId.getText())) {
      guestId = Integer.parseInt(tfGuestId.getText());
    }

    if (fromDate.after(toDate)) {
      POSMessageDialog.showError(Application.getInstance().getBackOfficeWindow(), com.floreantpos.POSConstants.FROM_DATE_CANNOT_BE_GREATER_THAN_TO_DATE_);
      return;
    }

    try {
      reportPanel.removeAll();
      reportPanel.revalidate();

      if (report != null) {
        int reportType = cbReportType.getSelectedIndex();

        report.setReportType(reportType);
        report.setStartDate(fromDate);
        report.setEndDate(toDate);
        
        if (isGuestReport) {
          report.setGuestId(guestId);
        }

        report.refresh();

        if (report != null && report.getViewer() != null) {
          reportPanel.add(report.getViewer());
          reportPanel.revalidate();
        }
      }

    } catch (Exception e) {
      Logger.getLogger(ReportViewer.class.getName()).log(Level.SEVERE, null, e);
      MessageDialog.showError(com.floreantpos.POSConstants.ERROR_MESSAGE, e);
    }
  }

  private javax.swing.JButton btnRefresh;
  private javax.swing.JComboBox cbReportType;
  private org.jdesktop.swingx.JXDatePicker dpEndDate;
  private org.jdesktop.swingx.JXDatePicker dpStartDate;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JLabel jLabel2;
  private javax.swing.JLabel jLabel3;
  private javax.swing.JLabel jLabel4;
  private javax.swing.JTextField tfGuestId;
  private com.floreantpos.swing.TransparentPanel reportConstraintPanel;
  private com.floreantpos.swing.TransparentPanel reportPanel;

  public Report getReport() {
    return report;
  }

  public void setReport(Report report) {
    this.report = report;
  }

}
